//
//  CollectionTripsViewController.m
//  chanyou
//
//  Created by leon on 15/1/8.
//  Copyright (c) 2015年 wanli. All rights reserved.
//

#import "CollectionTripsViewController.h"
#import "CollectionTripsTableViewCell.h"
#import "TripsModel.h"
#import "TripsDatabase.h"
#import "UIImageView+WebCache.h"
#import "ArticlesModel.h"
#import "TripsDetailViewController.h"
#import <ShareSDK/ShareSDK.h>
@interface CollectionTripsViewController ()<UITableViewDataSource, UITableViewDelegate>

@end

@implementation CollectionTripsViewController
- (void)dealloc
{
    [_collectTripsTableArray release];
    [_collectTripsView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createView];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.collectTripsTableArray = [NSMutableArray array];
        // 从数据库获取数据
        
        
        [ShareSDK getUserInfoWithType:ShareTypeSinaWeibo authOptions:nil result:^(BOOL result, id<ISSPlatformUser> userInfo, id<ICMErrorInfo> error) {
            if (result) {
                NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
                [def setObject:[userInfo uid] forKey:@"user"];
            }
        }];
        NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
//        NSInteger weiboId = [TripsDatabase searchWeiboId];
        NSArray *tripsArray = [TripsDatabase searchAllTravelsWithWeiboId:[[def objectForKey:@"user"] integerValue]];
//        if (weiboId == [[def objectForKey:@"user"] integerValue]){
            [self.collectTripsTableArray removeAllObjects];
            [_collectTripsTableArray addObjectsFromArray:tripsArray];
//        }
        [self.collectTripsView reloadData];
    }
    return self;
}
- (void)createView
{
    self.collectTripsView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - self.view.frame.size.height * 3 / 9 - 110)];
    _collectTripsView.dataSource = self;
    _collectTripsView.delegate = self;
    _collectTripsView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_collectTripsView registerClass:[CollectionTripsTableViewCell class] forCellReuseIdentifier:@"collectTripCell"];
    [self.view addSubview:_collectTripsView];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_collectTripsTableArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CollectionTripsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"collectTripCell"];
    TripsModel *trips = [[TripsModel alloc] init];
    trips = [_collectTripsTableArray objectAtIndex:indexPath.row];
    cell.titleLabel.text = trips.name;
    
    [cell.picImgView sd_setImageWithURL:[NSURL URLWithString:trips.front_cover_photo_url] placeholderImage:[UIImage imageNamed:@"placeHold"]];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120.0f;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TripsDetailViewController *tripsDetail = [[TripsDetailViewController alloc] init];
    TripsModel *trips = [[TripsModel alloc] init];
    trips = [_collectTripsTableArray objectAtIndex:indexPath.row];
    tripsDetail.IdNum = [trips.id integerValue];
    [self.navigationController pushViewController:tripsDetail animated:YES];
    [tripsDetail release];
}

#pragma mark - 
#pragma mark 滑动删除
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    TripsModel *trips = [_collectTripsTableArray objectAtIndex:indexPath.row];
    if (editingStyle == UITableViewCellEditingStyleDelete) {
//        [TripsDatabase deleteFromTrips:[trips.id integerValue]];
        NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
        [TripsDatabase deleteFromTrips:[trips.id integerValue] weiboId:[[def objectForKey:@"user"] integerValue]];
        [_collectTripsTableArray removeObjectAtIndex:indexPath.row];
    }
    [tableView reloadData];
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"删除";
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
